package cc.ewell.esb.authenticating.util;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.URLEncoder;
import org.apache.commons.io.IOUtils;











public class TcpUtil
{
    private int port;
    private String host;
    private Socket socket;
    private BufferedReader bufferedReader;
    private BufferedWriter bufferedWriter;

    public TcpUtil(String host, int port) throws Exception {
        this.host = host;
        this.port = port;




        this.socket = new Socket(this.host, this.port);
    }






    public String sendGet() throws IOException {
        String paramXml = IOUtils.toString(TcpUtil.class.getResourceAsStream("/请求格式-EMR.xml"));
        String param = "method=authenticateReqResp&paramXml=" + paramXml;
        long beginTime = System.currentTimeMillis();
        String requestUrlPath = "/";

        OutputStreamWriter streamWriter = new OutputStreamWriter(this.socket.getOutputStream());
        this.bufferedWriter = new BufferedWriter(streamWriter);
        this.bufferedWriter.write("GET " + requestUrlPath + " HTTP/1.1\r\n");
        this.bufferedWriter.write("Host: " + this.host + "\r\n");
        this.bufferedWriter.write("\r\n");
        this.bufferedWriter.flush();

        BufferedInputStream streamReader = new BufferedInputStream(this.socket.getInputStream());
        this.bufferedReader = new BufferedReader(new InputStreamReader(streamReader, "utf-8"));
        String line = null;
        while ((line = this.bufferedReader.readLine()) != null) {
            System.out.println(line);
        }
        this.bufferedReader.close();
        this.bufferedWriter.close();
        this.socket.close();
        return line;
    }

    public String sendPost(String method, String paramXml) throws IOException {
        String path = "/integration-esb-authenticating-server/integration/interfaces/reqreply.do";
        String data = String.valueOf(URLEncoder.encode("method", "utf-8")) + "=" + URLEncoder.encode(method, "utf-8") + "&" + URLEncoder.encode("paramXml", "utf-8") + "=" + URLEncoder.encode(paramXml, "utf-8");


        OutputStreamWriter streamWriter = new OutputStreamWriter(this.socket.getOutputStream(), "utf-8");
        this.bufferedWriter = new BufferedWriter(streamWriter);
        this.bufferedWriter.write("POST " + path + " HTTP/1.1\r\n");
        this.bufferedWriter.write("Host: " + this.host + "\r\n");
        this.bufferedWriter.write("Content-Length: " + data.length() + "\r\n");
        this.bufferedWriter.write("Content-Type: application/x-www-form-urlencoded\r\n");
        this.bufferedWriter.write("\r\n");
        this.bufferedWriter.write(data);

        this.bufferedWriter.write("\r\n");
        this.bufferedWriter.flush();

        BufferedInputStream streamReader = new BufferedInputStream(this.socket.getInputStream());
        this.bufferedReader = new BufferedReader(new InputStreamReader(streamReader, "utf-8"));
        String line = null;
        while ((line = this.bufferedReader.readLine()) != null) {
            if (line != null && line.indexOf(line.indexOf("成功")) >= 0)
            {
                return line;
            }
        }
        this.bufferedReader.close();
        this.bufferedWriter.close();
        this.socket.close();
        return null;
    }

    public static void main(String[] args) throws Exception {
        String paramXml = IOUtils.toString(TcpUtil.class.getResourceAsStream("/请求格式-源表-EMR.xml"));
        long beginTime = System.currentTimeMillis();



        TcpUtil forHttpTest = new TcpUtil("www.esb-auth.com", 7003);

        try {
            String str = forHttpTest.sendPost("authenticateReqResp", paramXml);
            System.out.println("本次调用耗时" + (System.currentTimeMillis() - beginTime) + "毫秒 ");
            System.out.println("结果：" + str);
        } catch (IOException e) {

            e.printStackTrace();
        }
    }
}


/* Location:              C:\Users\taoo6\Desktop\integrationEsbAuthenticating-client-1.0.jar!\cc\ewell\esb\authenticatin\\util\TcpUtil.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.1.3
 */